package com.qf.mao.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 啾咪小魔王
 * @date 2022/4/6 16:33
 * @since JDK 1.8
 */
@SuppressWarnings("all")
@WebFilter("/*")
public class PermissFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestURI = request.getRequestURI();
        if (requestURI.contains("UserServlet") || requestURI.contains("easyui") || requestURI.contains("h-ui")) {
            //登录或者静态资源相关，直接放行
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            //检查用户是否登录
            Object loginUser = request.getSession().getAttribute("loginUser");
            if (loginUser == null) {
                //说明用户没有登录，跳转到登录页面
              response.sendRedirect("/studentTest/UserServlet");
            }else{
                //说明用户已经登录了
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }
    }

    @Override
    public void destroy() {

    }
}
